<?php
namespace App\Module\Admin\Service;

use App\Amqp\Producer\ExcellentProducer;
use App\Constants\Constant;
use App\Exception\AppException;
use App\Model\Foot;
use App\Model\Resource;
use App\Module\Admin\Request\Foot\CreateRequest;
use App\Module\Admin\Request\Foot\UpdateRequest;
use Hyperf\Amqp\Producer;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class FootService{

    /**
     * @Inject()
     * @var ChildService $childService
     */
    private $childService;

    /**
     * @Inject()
     * @var TeacherService $teacherService
     */
    private $teacherService;

    /**
     * @Inject()
     * @var UserService $userService
     */
    private $userService;

    /**
     * @Inject()
     * @var Producer $producer
     */
    private $producer;

    public function search($child_id,$child_name,$is_public,$is_excellent,$source,$source_name,$start_time,$end_time,$pagesize){
        $query = Foot::query()->latest("foot_id");

        if(!empty($child_id)){
            $query->where("child_id",$child_id);
        }
        if(!empty($child_name)){
            $query->where("child_name",'like','%' . $child_name .'%');
        }
        if(!empty($is_public)){
            $query->where("is_public",$is_public);
        }
        if(!empty($is_excellent)){
            $query->where("is_excellent",$is_excellent);
        }
        if(!empty($source)){
            $query->where("source",$source);
        }
        if(!empty($source_name)){
            $query->where("source_name",'like','%' . $source_name .'%');
        }
        if(!empty($start_time)){
            $query->whereDate("add_time",">=" ,$start_time);
        }
        if(!empty($end_time)){
            $query->whereDate("add_time","<=" ,$end_time);
        }

        return $query->paginate($pagesize);
    }

    public function create(CreateRequest $request){
        return Db::transaction(function () use($request){
            $child = $this->childService->getById($request->input("child_id"));
            $foot = new Foot();
            $foot->fill($request->all());
            $foot->setAttribute("child_name",$child->getAttribute("name"));
            $foot->setAttribute("child_photo",$child->getAttribute("photo"));
            if($request->input("source") == Constant::SOURCE_TEACHER){
                $foot->setAttribute("source_name",$this->teacherService->getById($request->input("source_id"))->getAttribute("name"));
            }else if($request->input("source") == Constant::SOURCE_USER){
                $foot->setAttribute("source_name",$this->userService->getById($request->input("source_id"))->getAttribute("username"));
            }
            $foot->save();

            if(!$request->has(["resources"])){
                return;
            }

            $resources = $request->input("resources");
            foreach ($resources as $v){
                $resource = new Resource();
                $resource->setAttribute("source_id",$foot->foot_id);
                $resource->setAttribute("uri",$v);
                $resource->setAttribute("origin",Constant::ORIGIN_FOOT);
                $resource->save();
            }
        });
    }

    public function getById($id){
        return Foot::query()->with("resources")->find($id);
    }

    public function update(UpdateRequest $request,$id){
        return Db::transaction(function ()use($request,$id){
            $child = $this->childService->getById($request->input("child_id"));
            $foot = Foot::query()->findOrFail($id);
            $foot->fill($request->all());
            $foot->setAttribute("child_name",$child->getAttribute("name"));
            $foot->setAttribute("child_photo",$child->getAttribute("photo"));
            if($request->input("source") == Constant::SOURCE_TEACHER){
                $foot->setAttribute("source_name",$this->teacherService->getById($request->input("source_id"))->getAttribute("name"));
            }else if($request->input("source") == Constant::SOURCE_USER){
                $foot->setAttribute("source_name",$this->userService->getById($request->input("source_id"))->getAttribute("username"));
            }
            $foot->save();

            Resource::query()->where([['source_id',$id],['origin',Constant::ORIGIN_FOOT]])->delete();

            if(!$request->has(["resources"])){
                return;
            }
            $resources = $request->input("resources");
            foreach ($resources as $v){
                $resource = new Resource();
                $resource->setAttribute("source_id",$id);
                $resource->setAttribute("uri",$v);
                $resource->setAttribute("origin",Constant::ORIGIN_FOOT);
                $resource->save();
            }
        });
    }

    public function delete($id){
        return Db::transaction(function () use ($id){
            $model = Foot::query()->findOrFail($id);
            $model->delete();
            if($model->resource_type != 0) {
                Resource::query()->where([['source_id', $id], ['origin', Constant::ORIGIN_FOOT]])->delete();
            }
        });
    }

    public function excellent($id){
        $model = Foot::query()->findOrFail($id);

        if($model->source != Constant::SOURCE_USER){
            throw new AppException("非用户发布无法设置为优秀足迹");
        }

        $model->setAttribute("is_excellent",2);
        $model->save();
        return $this->producer->produce(new ExcellentProducer(['user_id'=>$model->source_id,'source_id'=>$id]));
    }
}